package com.sylar.happyreader.page;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;
import android.util.TypedValue;

public class BookPage {
	private ArrayList<ArrayList<String>> lists = new ArrayList<ArrayList<String>>();
	private int m_totalPage = 0;
	private int m_curPage = 0;
	private Bitmap m_book_bg = null;
	private int mWidth;
	private int mHeight;

	private List<String> m_lines = new ArrayList<String>();


	private float m_fontSize;
	int m_iFontHeight;
//	private int m_textColor = Color.BLACK;
//	private int m_backColor = 0xffff9e85;
	private int marginWidth = 0;
	private int marginHeight = 0;
	private float scale;

	private int mLineCount;
	private float mVisibleHeight;
	private float mVisibleWidth;
	private boolean m_isfirstPage, m_islastPage;

	private Paint mPaint;


	public BookPage(Context context) {
		mWidth = context.getResources().getDisplayMetrics().widthPixels;
		mHeight = context.getResources().getDisplayMetrics().heightPixels;
		scale = context.getResources().getDisplayMetrics().scaledDensity;

		m_fontSize = getRawSize(context, TypedValue.COMPLEX_UNIT_DIP, 20);
		marginWidth = (int) (20 * scale);
		marginHeight = (int) (20 * scale);

		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setStyle(Style.STROKE);
		mPaint.setTextSize(m_fontSize);
		FontMetrics fm = mPaint.getFontMetrics();
		m_iFontHeight = (int) Math.ceil(fm.descent - fm.top) + 4;
		mVisibleWidth = mWidth - marginWidth * 2;
		mVisibleHeight = mHeight - marginHeight * 2;
		mLineCount = (int) (mVisibleHeight / m_iFontHeight);

	}

	public float getRawSize(Context context, int unit, float size) {
		Resources r;

		if (context == null)
			r = Resources.getSystem();
		else
			r = context.getResources();

		return TypedValue.applyDimension(unit, size, r.getDisplayMetrics());
	}

	public void openbook(String sb) throws IOException {
		m_curPage = 0;
		m_lines.clear();
		lists.clear();
		lists = getPageContentStringInfo(mPaint, sb.toString(), mLineCount, mVisibleWidth);
		m_totalPage = lists.size();
	}

	private ArrayList<ArrayList<String>> getPageContentStringInfo(Paint m_paint, String content, int pageLines, float pageWidth) {

		char ch;
		int w = 0;
		int istart = 0;
		int lineNum = 0;
		ArrayList<ArrayList<String>> contentList = new ArrayList<ArrayList<String>>();
		ArrayList<String> cl = null;
		for (int i = 0; i < content.length(); i++) {
			if (cl == null)
				cl = new ArrayList<String>();
			ch = content.charAt(i);
			float[] widths = new float[1];
			String srt = String.valueOf(ch);
			m_paint.getTextWidths(srt, widths);
			if (ch == 32 || ch == 9 || ch == 0 || ch == 13 || ch == 12288) {
				lineNum++;
				cl.add(content.substring(istart, i));
				istart = i + 1;
				w = 0;
			}

			else {
				w += (int) (Math.ceil(widths[0]));
				if (w > pageWidth) {
					lineNum++;
					cl.add(content.substring(istart, i));
					istart = i;
					i--;
					w = 0;
				} else {
					if (i == (content.length() - 1)) {
						lineNum++;
						cl.add(content.substring(istart, content.length()));
					}
				}
			}
			if (lineNum == pageLines || i == (content.length() - 1)) {
				contentList.add(cl);
				cl = null;
				lineNum = 0;
			}
		}
		return contentList;
	}

	private List<String> pageDown() {
		List<String> list = new ArrayList<String>();
		if (lists != null && m_curPage < lists.size())
			list = lists.get(m_curPage);
		return list;
	}
	
	public List<String> pageDown(int pageNo) {
		List<String> list = new ArrayList<String>();
		if (lists != null && pageNo < lists.size() && pageNo >= 0)
			list = lists.get(pageNo);
		return list;
	}

	public void prePage(){
		if (m_curPage <= 0) {
			m_curPage = 0;
			m_isfirstPage = true;
			return;
		} else
			m_isfirstPage = false;
		m_curPage--;
		m_lines = pageDown();
	}

	public void nextPage(){
		if (m_curPage + 1 >= m_totalPage) {
			m_islastPage = true;
			return ;
		} else
			m_islastPage = false;
		m_curPage++;
		m_lines = pageDown();
	}

	public void onDraw(Canvas c) {
//		if (m_lines.size() == 0)
//			m_lines = pageDown();
//		if (m_lines.size() > 0) {
//			if (m_book_bg == null)
//				c.drawColor(m_backColor);
//			else
//				c.drawBitmap(m_book_bg, 0, 0, null);
//			int y = marginHeight;
//			for (String strLine : m_lines) {
//				y += m_fontSize + line;
//				c.drawText(strLine, marginWidth, y, mPaint);
//			}
//		}
		// float fPercent = (float) (m_mbBufBegin * 1.0 / m_mbBufLen);
		// DecimalFormat df = new DecimalFormat("#0.0");
		// String strPercent = df.format(fPercent * 100) + "%";
		// Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		// mPaint.setTextAlign(Align.LEFT);
		// mPaint.setTextSize(m_fontSize-5);
		// mPaint.setColor(m_textColor);
		// int nPercentWidth = (int) mPaint.measureText("999.9%") + 1;
		// c.drawText(strPercent, mWidth - nPercentWidth, mHeight - 5, mPaint);

	}

	public void setBgBitmap(Bitmap BG) {
		m_book_bg = BG;
	}

	public boolean isfirstPage() {
		return m_isfirstPage;
	}

	public boolean islastPage() {
		return m_islastPage;
	}
	
	public int getM_totalPage() {
		return m_totalPage;
	}

	public int getM_curPage() {
		return m_curPage;
	}
	
	public List<String> getM_lines() {
		return m_lines;
	}

}
